package com.weipu.common.facade.support.service.callback;

import java.io.Serializable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.weipu.common.facade.support.service.task.BaseServiceTask;
import com.weipu.common.subject.MessageSubjectImpl;

/**
 * 后台ServieTask数据接收，用于异步数据调用回调，回调数据通过{@link TaskDataBroadcastReceiver#onReceiveData(T extends Serializable)}返回
 * @author Administrator
 *
 * @param <T>
 */
public abstract class TaskDataBroadcastReceiver<T extends Serializable> extends BroadcastReceiver
{
    
    /**
     * 广播监听地址
     */
    public static final String ACTION_URL =
        "com.weipu.common.facade.support.service.callback.taskdatabroadcastreceiver";
    
    /**
     * 数据地址
     */
    public static final String INTENT_DATA = "data_from_service_task";
    
    /**
     * 回调标志位: 多个场景公用一个接收器时通过what区分
     * 通过{@link BaseServiceTask#scheduledExecutionTime()}}设置回调标识what
     */
    public static final String INTENT_OPTION_WHAT = "data_from_what_service_task";
    
    /**
     * 返回广播数据(在onReceive之后被调用)
     * 主线程操作
     * @return
     */
    public abstract void onReceiveData(T receiver);
    
    @SuppressWarnings("unchecked")
    @Override
    public void onReceive(Context context, Intent intent)
    {
        onReceiveData((T)(intent.getSerializableExtra(INTENT_DATA)));
        int what = intent.getIntExtra(INTENT_OPTION_WHAT,0);
        switch (what) {
		case BaseServiceTask.FROM_NOTICE_FEEDBACK:
			int noticeType = intent.getIntExtra(INTENT_DATA, 0);
			MessageSubjectImpl.getMessageSubjectImplInstance().notice(1, noticeType);
			break;

		default:
			break;
		}
    }
    
}
